/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.spoon.dialog;

import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.core.FormDataBuilder;
import org.pentaho.di.ui.core.PropsUI;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.core.gui.WindowProperty;
import org.pentaho.di.ui.trans.step.BaseStepDialog;

public class NewSubtransDialog extends Dialog {
    private static Class<?> PKG = NewSubtransDialog.class;
    private Button wShow;
    private Label wlInfo;
    private Label wiInfo;
    private FormData fdlInfo;
    private FormData fdiInfo;
    private FormData fdShowButton;
    private Button wOK;
    private Listener lsOK;
    private Shell shell;
    private PropsUI props = PropsUI.getInstance();
    boolean doNotShow = false;

    public NewSubtransDialog(Shell parent, int style) {
        super(parent, style);
    }

    public boolean open() {
        Shell parent = this.getParent();
        Display display = parent.getDisplay();
        this.shell = new Shell(parent, 3296);
        this.props.setLook(this.shell);
        FormLayout formLayout = new FormLayout();
        formLayout.marginWidth = 15;
        formLayout.marginHeight = 15;
        this.shell.setLayout(formLayout);
        this.shell.setText(BaseMessages.getString(PKG, "NewSubtransDialog.Title", new String[0]));
        this.shell.setImage(GUIResource.getInstance().getImageLogoSmall());
        this.wiInfo = new Label(this.shell, 0);
        this.wiInfo.setImage(display.getSystemImage(2));
        this.props.setLook(this.wiInfo);
        this.fdiInfo = new FormData();
        this.fdiInfo.left = new FormAttachment(0, 0);
        this.fdiInfo.top = new FormAttachment(0, 0);
        this.wiInfo.setLayoutData(this.fdiInfo);
        this.wlInfo = new Label(this.shell, 64);
        this.wlInfo.setText(BaseMessages.getString(PKG, "NewSubtransDialog.TransCreated", new String[0]));
        this.props.setLook(this.wlInfo);
        this.fdlInfo = new FormData();
        this.fdlInfo.left = new FormAttachment(this.wiInfo, 15);
        this.fdlInfo.right = new FormAttachment(100, 0);
        this.fdlInfo.width = 320;
        this.fdlInfo.top = new FormAttachment(this.wiInfo, 0, 128);
        this.fdlInfo.bottom = new FormAttachment(this.wiInfo, 0, 1024);
        this.wlInfo.setLayoutData(this.fdlInfo);
        this.wShow = new Button(this.shell, 32);
        this.wShow.setText(BaseMessages.getString(PKG, "NewSubtransDialog.DoNotShowAgain", new String[0]));
        this.wShow.setLayoutData((new FormDataBuilder()).left().result());
        this.props.setLook(this.wShow);
        this.fdShowButton = new FormData();
        this.fdShowButton.left = new FormAttachment(this.wlInfo, 0, 16384);
        this.fdShowButton.right = new FormAttachment(this.wlInfo, 0, 131072);
        this.fdShowButton.top = new FormAttachment(this.wlInfo, 15, 1024);
        this.wShow.setLayoutData(this.fdShowButton);
        this.wOK = new Button(this.shell, 8);
        this.wOK.setText(BaseMessages.getString(PKG, "System.Button.OK", new String[0]));
        FormData fdOk = new FormData();
        fdOk.right = new FormAttachment(100, 0);
        fdOk.top = new FormAttachment(this.wShow, 30, 1024);
        this.wOK.setLayoutData(fdOk);
        this.lsOK = new Listener() {
            public void handleEvent(Event e) {
                NewSubtransDialog.this.close();
            }
        };
        this.wOK.addListener(13, this.lsOK);
        this.shell.addShellListener(new ShellAdapter() {
            public void shellClosed(ShellEvent e) {
                NewSubtransDialog.this.close();
            }
        });
        BaseStepDialog.setSize(this.shell);
        this.shell.open();

        while(!this.shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        return this.doNotShow;
    }

    public void dispose() {
        this.props.setScreen(new WindowProperty(this.shell));
        this.shell.dispose();
    }

    private void close() {
        this.doNotShow = this.wShow.getSelection();
        this.dispose();
    }
}
